package com.kaishengit.service;

import java.text.ParseException;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;



import com.kaishengit.dao.DealDao;
import com.kaishengit.dao.TaskDao;
import com.kaishengit.dao.TaskTypeDao;
import com.kaishengit.pojo.Deal;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.TaskType;
import com.kaishengit.pojo.User;
import com.kaishengit.util.DataUtil;

import freemarker.template.utility.DateUtil;
@Transactional
public class TaskService {
	private TaskTypeDao taskTypeDao;
	private TaskDao taskDao;
	private DealDao dealDao;
	public Task save(String taskname, String selecttime, String leibie,
			String taskType, String settimeinput,User user, String id) throws ParseException {
		Deal deal = dealDao.findByUnique("id",id);
		TaskType type;
		if(leibie.equals("none")){
			type = null;
		}else{
			 type= taskTypeDao.findByUnique("id",leibie);
		}
		
		if(selecttime==null && settimeinput!=null ){
			Task task = new Task();
			task.setDeal(deal);
			task.setContent(taskname);
			task.setEndtime(DataUtil.getJutiEndTime(settimeinput));
			task.setStarttime(DataUtil.getJutiStartTime(settimeinput));
			//这是设置具体的是关于个人 或者是公司的 联系人  或者是 deal
			task.setType(taskType);
			//设置次task 是打电话  还是 email
			task.setTaskType(type);
			task.setUser(user);
			taskDao.save(task);
			String dataType = DataUtil.getDataType(task.getStarttime(),task.getEndtime());
			task.setDataType(dataType);
			//task.setUser(null);
			return task;
		}else if(settimeinput==null&&selecttime!=null){
			Task task = new Task();
			String endtime = null;
			task.setDeal(deal);
		    String starttime = null;
			if(selecttime.equals("today")){
				endtime = DataUtil.getTodayEndTime();
				starttime = DataUtil.getTodayStartTime();
			}else if(selecttime.equals("tomorrow")){
				endtime = DataUtil.getTomorrowEndTime();
				starttime = DataUtil.getTomorrowStartTime();
			}else if(selecttime.equals("thisweek")){
				 starttime= DataUtil.getThisweekStartTime();
				 endtime = DataUtil.getThisweekEndTime();
			}else if(selecttime.equals("nextweek")){
				endtime = DataUtil.getNextweekEndTime();
				starttime = DataUtil.getNextweekStartTime();
			}else if(selecttime.equals("later")){
				starttime = DataUtil.getNextweekEndTime();
				endtime="2050-10-12 00:00:00";
			}
			task.setContent(taskname);
			task.setEndtime(endtime);
			task.setStarttime(starttime);
			//这是设置具体的是关于个人 或者是公司的 联系人  或者是 deal
			
			task.setType(taskType);
			//设置次task 是打电话  还是 email
			if(type!=null){
				task.setTaskType(type);
			}
			task.setUser(user);
			taskDao.save(task);
			task.setDataType(selecttime);
			//task.setUser(null);
			return task;
		}else{
			return null;
		}
		
	}
	//查找某个交易所有的日程安排，task表type列里面存的是交易的id
	public List<Task> findTaskByType(String id) throws ParseException {
		List<Task> list = taskDao.findAllByProperty2("type",id);
		for(Task task:list){
			String dataType = DataUtil.getDataType(task.getStarttime(),task.getEndtime());
			task.setDataType(dataType);
		}
		return list;
		
	}
	public void setTaskTypeDao(TaskTypeDao taskTypeDao) {
		this.taskTypeDao = taskTypeDao;
	}
	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}
	public void delById(String id) {
		
		taskDao.del(id);
		
	}
	public List<Task> findAllByUser(User user) throws ParseException {
		// TODO Auto-generated method stub
		List<Task> list = taskDao.findAllByProperty3("user", user);
		for(Task task:list){
			String dataType = DataUtil.getDataType(task.getStarttime(),task.getEndtime());
			task.setDataType(dataType);
		}
		return list;
	}
	public void edit(String id, String taskname, String selecttime,
			String settimeinput, String taskType) {
		Task task = taskDao.findByUnique("id",id);
		TaskType type;
		if(taskType.equals("none")){
			type = null;
		}else{
			 type= taskTypeDao.findByUnique("id",taskType);
		}
		if(selecttime.equals("") ){
			task.setContent(taskname);
			task.setEndtime(DataUtil.getJutiEndTime(settimeinput));
			task.setStarttime(DataUtil.getJutiStartTime(settimeinput));
			//设置次task 是打电话  还是 email
			task.setTaskType(type);
			taskDao.save(task);
		}else if(settimeinput.equals("")){
			String endtime = null;
		    String starttime = null;
			if(selecttime.equals("today")){
				endtime = DataUtil.getTodayEndTime();
				starttime = DataUtil.getTodayStartTime();
			}else if(selecttime.equals("tomorrow")){
				endtime = DataUtil.getTomorrowEndTime();
				starttime = DataUtil.getTomorrowStartTime();
			}else if(selecttime.equals("thisweek")){
				 starttime= DataUtil.getThisweekStartTime();
				 endtime = DataUtil.getThisweekEndTime();
			}else if(selecttime.equals("nextweek")){
				endtime = DataUtil.getNextweekEndTime();
				starttime = DataUtil.getNextweekStartTime();
			}else if(selecttime.equals("later")){
				starttime = DataUtil.getNextweekEndTime();
				endtime="2050-10-12 00:00:00";
			}
			task.setContent(taskname);
			task.setEndtime(endtime);
			task.setStarttime(starttime);
			//设置次task 是打电话  还是 email
			if(type!=null){
				task.setTaskType(type);
			}
			taskDao.save(task);
			
		}
		
	}
	public void setDealDao(DealDao dealDao) {
		this.dealDao = dealDao;
	}
	
	
	
	
	
}
